package com.loto.insurance.oem.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.CustFeeFlow;
import com.loto.insurance.oem.web.entity.Pager;
import com.loto.insurance.oem.web.entity.SessionUser;
import com.loto.insurance.oem.web.enumeration.AuthorityRoleEnum;
import com.loto.insurance.oem.web.enumeration.MenuEnum;
import com.loto.insurance.oem.web.service.CustFeeFlowService;
import com.loto.insurance.oem.web.service.CustInfoService;
import com.loto.utils.DateUtils;
import com.loto.utils.StringUtils;

@Controller
@SessionAttributes("oem_user")
public class AccountController {
  @Autowired
  CustFeeFlowService custFeeFlowService;

  @Autowired
  CustInfoService custInfoService;

  @OEMAuthority
  @RequestMapping(value = "/account/consumption", method = RequestMethod.GET)
  public String accountConsumption(
	  @RequestParam(value = "consumeType", required = false) String consumeType,
	  @RequestParam(value = "consumeBegin", required = false) String consumeBegin,
	  @RequestParam(value = "consumeEnd", required = false) String consumeEnd,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (consumeType != null && !consumeType.isEmpty())
	  params.put("consumeType", consumeType);
	if (consumeBegin != null && consumeEnd != null
	    && DateUtils.isDate(consumeBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(consumeEnd, "yyyy-MM-dd")) {
	  params.put("consumeBegin", consumeBegin);
	  params.put("consumeEnd", consumeEnd);
	}

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}
	params.put("custId", sessionUser.getUserId());
	model.addAllAttributes(params);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<CustFeeFlow> flowList = custFeeFlowService.findFlowByCust(params);

	pager.setRowCount(custFeeFlowService.findFlowByCustCount(params));

	model.addAttribute("pager", pager);
	model.addAttribute("flowList", flowList);

	model.addAttribute("usefulFee",
	    custInfoService.getUsefulfee(sessionUser.getUserId()));

	model.addAttribute("pageId", MenuEnum.ACCOUNT_CONSUM.getIndex());
	return "account/consum_list";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.USERS)
  @RequestMapping(value = "/trade/consumption", method = RequestMethod.GET)
  public String tradeConsumption(
	  @RequestParam(value = "custName", required = false) String custName,
	  @RequestParam(value = "consumeType", required = false) String consumeType,
	  @RequestParam(value = "consumeBegin", required = false) String consumeBegin,
	  @RequestParam(value = "consumeEnd", required = false) String consumeEnd,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (custName != null && !custName.isEmpty())
	  params.put("custName", custName);
	if (consumeType != null && !consumeType.isEmpty())
	  params.put("consumeType", consumeType);
	if (consumeBegin != null && consumeEnd != null
	    && DateUtils.isDate(consumeBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(consumeEnd, "yyyy-MM-dd")) {
	  params.put("consumeBegin", consumeBegin);
	  params.put("consumeEnd", consumeEnd);
	}

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}
	params.put("custId", sessionUser.getUserId());
	model.addAllAttributes(params);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<CustFeeFlow> flowList = custFeeFlowService.findFlow(params);

	pager.setRowCount(custFeeFlowService.findFlowCount(params));

	model.addAttribute("pager", pager);
	model.addAttribute("flowList", flowList);

	model.addAttribute("pageId", MenuEnum.TRADE_CONSUM.getIndex());
	return "trade/consum_list";
  }

  @OEMAuthority
  @RequestMapping(value = "/account/pwd")
  public String changePwd(Model model) {
	model.addAttribute("pageId", MenuEnum.ACCOUNT_PWD.getIndex());
	return "account/change_pwd";
  }
}
